<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @reset="handleReset" @submit="handleSubmit">
      <template #anchorSelect="{ model, field }">
        <n-date-picker
          v-model:formatted-value="model[field]"
          clearable
          type="daterange"
          value-format="yyyy-MM-dd"
        />
      </template>
    </BasicForm>

    <BasicTable
      ref="actionRef"
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      :scroll-x="2048"
      @update:checked-row-keys="onCheckedRow"
    >
      <template #tableTitle>
        <span><b>汇总</b></span> <b>:</b>
        <span style="color: red; margin-left: 10px"
          ><b>{{ totalAmount }}</b></span
        >
      </template>
      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>
  </n-card>
</template>

<script lang="ts" setup>
  import { onMounted, reactive, ref, unref } from 'vue';
  import { useDialog, useMessage } from 'naive-ui';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { columns } from './columns';
  import { useRouter } from 'vue-router';
  import { getReflectFlow } from '@/api/player/reflectFlow';
  import { formatToDate } from '@/utils/dateUtil';

  const schemas: FormSchema[] = [
    {
      field: 'userId',
      labelMessage: '用户ID',
      component: 'NInput',
      label: '用户ID',
      componentProps: {
        placeholder: '用户ID',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'roleId',
      component: 'NInput',
      label: '角色ID',
      componentProps: {
        placeholder: '角色ID',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'orderNo',
      component: 'NInput',
      label: '提现单号',
      componentProps: {
        placeholder: '提现单号',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'payWay',
      component: 'NSelect',
      label: '提现方式',
      componentProps: {
        placeholder: '提现方式',

        options: (function () {
          var arr = [
            {
              label: '微信',
              value: 'wechat',
            },
            {
              label: '支付宝',
              value: 'alipay',
            },
          ];
          return arr;
        })(),
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'timeRange',
      label: '提现时间',
      componentProps: {
        placeholder: '提现时间',
      },
      slot: 'anchorSelect',
    },
    {
      field: 'status',
      component: 'NSelect',
      label: '提现状态',
      defaultValue: 'true',
      componentProps: {
        options: (function () {
          var arr = [
            {
              label: '已通过',
              value: 'true',
            },
            {
              label: '已驳回',
              value: 'reject',
            },
            {
              label: '已退还',
              value: 'refund',
            },
            {
              label: '待审核',
              value: 'fail',
            },
          ];
          return arr;
        })(),
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
  ];
  var totalAmount = 0;
  const router = useRouter();
  const formRef: any = ref(null);
  const message = useMessage();
  const dialog = useDialog();
  const actionRef = ref();

  const showModal = ref(false);
  const formBtnLoading = ref(false);
  const formParams = reactive({
    userId: 0,
    payStatus: 'success',
  });

  const params = ref({
    userId: 0,
  });
  // const createSummary: DataTableCreateSummary = (pageData) => {
  //   return {
  //     name: {
  //       value: h(
  //         'span',
  //         { style: { color: 'red' } }
  //         // (pageData as RowData[]).reduce((prevValue, row) => prevValue + row.age, 0)
  //       ),
  //       colSpan: 3,
  //     },
  //   };
  // };
  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  const loadDataTable = async (res) => {
    if (!formParams.userId) {
      formParams.userId = 0;
    } else {
      formParams.userId = parseInt(formParams.userId);
    }
    const param = { ...formParams, ...res };
    console.log(param);
    const data = await getReflectFlow(param);
    console.log(data);
    totalAmount = data.totalAmount;
    return data;
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  // function createSummary() {}

  function handleDetail(record: Recordable) {
    // console.log('点击了编辑', record);
    // router.push({name: 'rechargeFlow', params: {id: record.userId}});
  }

  function handleSubmit(values: Recordable) {
    console.log('submit', values);
    let tempParams = unref(formParams);
    tempParams = Object.assign(tempParams, values);
    if (values.makeDate) {
      let [startTime, endTime]: [number, number] = values.makeDate;
      let startTimeS = formatToDate(new Date(startTime));
      let endTimeS = formatToDate(new Date(endTime));
      tempParams = Object.assign(tempParams, { startTime: startTimeS, endTime: endTimeS });
    }
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }

  onMounted(async () => {
    // registerHandler(ItemTableSwitchActions);
    // const chanList = await getChanList();
    // registChanArray.value = chanList.map((chan) => {
    //   return {
    //     label: chan.chanName,
    //     value: chan.chanId
    //   }
    // });
  });
</script>

<style lang="less" scoped></style>
